#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <stack>

using namespace std;

//题目要求加减运算
int calculate(string s) {
    stack<int> ops;
    ops.push(1);
    int sign = 1;

    int ret = 0;
    int n = s.length();
    int i = 0;
    while (i < n) {
        if (s[i] == ' ') {
            i++;
        } else if (s[i] == '+') {
            sign = ops.top();
            i++;
        } else if (s[i] == '-') {
            sign = -ops.top();
            i++;
        } else if (s[i] == '(') {
            ops.push(sign);
            i++;
        } else if (s[i] == ')') {
            ops.pop();
            i++;
        } else {
            long num = 0;
            while (i < n && s[i] >= '0' && s[i] <= '9') {
                num = num * 10 + s[i] - '0';
                i++;
            }
            ret += sign * num;
        }
    }
    return ret;
}

int cal(vector<int> &nst, vector<char> &sst)
{
    int r;
    switch (sst[sst.size() - 1])
    {
    case '+':
        r = *(nst.end() - 2) + *(nst.end() - 1);
        break;

    case '-':
        r = *(nst.end() - 2) - *(nst.end() - 1);
        break;

    case '*':
        r = *(nst.end() - 2) * *(nst.end() - 1);
        break;

    case '/':
        r = *(nst.end() - 2) / *(nst.end() - 1);
        break;
    
    default:
        break;
    }
    nst.pop_back(); nst.pop_back(); sst.pop_back();
    return r;
}

// 此函数可计算四则运算
int myCalculate(string s)
{
    vector<int> nstk; vector<char> sstk;
    int num = 0, p = 0;
    char preSign = '+';
    for (size_t i = 0; i < s.size(); i++)
    {
        if (isdigit(s[i]))
            num = num * 10 + int(s[i] - '0');
        if (!isdigit(s[i]) && s[i] != ' ' || i == s.size() - 1)
        {
            switch (preSign)
            {
                case '(':
                    p++;
                    sstk.push_back('(');
                    nstk.push_back(num);
                    break;
                case '+':
                    sstk.push_back('+');
                    nstk.push_back(num);
                    break;
                case '-':
                    sstk.push_back('+');
                    nstk.push_back(-num);
                    break;
                case '*':
                    sstk.push_back('+');
                    nstk.back() *= num;
                    break;
                case '/':
                    sstk.push_back('+');
                    nstk.back() /= num;
                    break;
                default:
                    while (sstk.back() != '(')
                    {
                        nstk.push_back(cal(nstk, sstk));
                    }
                    sstk.pop_back();
                    p--;
            }
            preSign = s[i];
            num = 0;
        }
    }
    while (p && sstk.back() != '(')
    {
        nstk.push_back(cal(nstk, sstk));
    }
    if(p) sstk.pop_back();
    return accumulate(nstk.begin(), nstk.end(), 0);
}


int main()
{
    string s = "(1+(4+5*4/2 +4)-3)+(6+8)";
    cout << calculate(s) << endl;
    return 0;
}
